package demo;

import cn.heapstack.util.JOptionParser;
import cn.heapstack.util.Option;

public class Demo {

	public static void main(String[] args) 
	{
		String helpMessage = "Useage: java Demo [Options]" ;
		Option helpOption = new Option("h", "help", "the helpMessage", false, false);
		
		Option queryOption = new Option("q", "query",
				"this option means we need to do some query", false, false);
		Option updateOption = new Option("u", "update",
				"this option means we need to do some update", false, false);
		Option idOption = new Option("id", "ID",
				"the key of query or update", true, true);

		JOptionParser optionParser = new JOptionParser();
		optionParser.addOption(helpOption);
		optionParser.addOption(idOption);
		optionParser.addOption(queryOption);
		optionParser.addOption(updateOption);

		try {
			optionParser.parse(args);

			//here we can do some complicated validation, logic checking
			//example: at least one of -q or -u should be specified
			if (!queryOption.isSpecified() && !updateOption.isSpecified()) {
				System.err.println("Neither query(-q) nor update(-u) is specified");
				if (!helpOption.isSpecified()) {
					optionParser.printUsage(helpMessage);
				}
				System.exit(0);
			}
			
			// here we can get each option's value
			System.out.println(idOption.getOptValue());
			
		} catch (IllegalArgumentException e) {
			System.err.println(e.getMessage());
			e.printStackTrace();
			if (!helpOption.isSpecified()) {
				optionParser.printUsage(helpMessage);
			}
			System.exit(0);
		}
	}
}
